<template>
  <el-dialog
    :visible.sync="dialogConf.show"
    :width="dialogConf.width"
    @closed="closed"
  >
    <template #title>
      <div class="dialogTitle">{{ dialogConf.title }}</div>
    </template>
    <my-form
      ref="form"
      :layout="elRows"
      :model="model"
      :form-conf="formConf"
      :btn-conf="btnConf"
      :row-conf="rowConf"
      class="configDialog"
      @btnClick="btnClick"
    >
    </my-form>
  </el-dialog>
</template>

<script>
import { delFormNullParam } from '@/components/common/common'
import MyForm from '@/components/base/myForm'

export default {
  name: 'Config',
  components: { MyForm },
  props: {
    dialogConf: {
      type: Object,
      default() {
        return { show: true, title: '配置', width: '800px' }
      }
    },
    row: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      model: {
        projectPath: null,
        projectPackage: null,
        tableName: null,
        tablePrefix: null,
        tableSuffix: null,
        entityPackage: null,
        voPackage: null,
        voSuffix: null,
        dtoPackage: null,
        dtoSuffix: null,
        mapperXmlPath: null,
        mapperPackage: null,
        mapperSuffix: null,
        servicePackage: null,
        serviceImplPackage: null,
        serviceSuffix: null,
        serviceImplSuffix: null,
        controllerPackage: null,
        controllerSuffix: null
      },
      btnConf: {
        buttons: [{ name: '提交', type: 'primary' }, { name: '取消' }]
      },
      formConf: {
        labelWidth: '110px',
        rules: {},
        labelPosition: 'top'
      },
      elRows: [
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'projectPath',
                label: '项目路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'projectPackage',
                label: '项目包名',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'tablePrefix',
                label: '表前缀',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'tableSuffix',
                label: '表后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'entityPackage',
                label: 'entity包路径',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'voPackage',
                label: 'vo包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'voSuffix',
                label: 'vo类名后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'dtoPackage',
                label: 'dto包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'dtoSuffix',
                label: 'dto类名后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'mapperXmlPath',
                label: 'xml路径',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'mapperPackage',
                label: 'mapper包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'mapperSuffix',
                label: 'mapper类名后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'servicePackage',
                label: 'service接口包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'serviceSuffix',
                label: 'service接口类名后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'serviceImplPackage',
                label: 'service实现类包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'serviceImplSuffix',
                label: 'service实现类名后缀',
                type: 'input'
              }
            }
          ]
        },
        {
          elCols: [
            {
              span: 12,
              elFormItem: {
                prop: 'controllerPackage',
                label: 'controller包路径',
                type: 'input'
              }
            },
            {
              span: 12,
              elFormItem: {
                prop: 'controllerSuffix',
                label: 'controller类名后缀',
                type: 'input'
              }
            }
          ]
        }
      ],
      rowConf: {
        gutter: 40
      }
    }
  },
  created() {},
  mounted() {
    if (this.row.config != null && this.row.config !== '') {
      const modelV = JSON.parse(this.row.config)
      for (const key in this.model) {
        this.model[key] = modelV[key]
      }
    }
  },
  methods: {
    async btnClick(btnItem) {
      try {
        if (btnItem.name === '提交') {
          let validFlag = false
          this.$refs.form.$refs.form.validate((valid) => {
            validFlag = valid
          })
          if (validFlag) {
            let params = Object.assign({}, this.model)
            params = delFormNullParam(params)
            btnItem.loading = true
            const data = await this.$axios.post(
              '/project/config?id=' + this.row.id,
              params
            )
            this.$message.success(data.msg)
            this.success()
          }
        } else if (btnItem.name === '取消') {
          this.closed()
        }
      } catch (e) {
        console.log(e)
      } finally {
        btnItem.loading = false
      }
    },
    closed(...args) {
      this.$emit('closed', ...args)
    },
    success(...args) {
      this.$emit('success', ...args)
    }
  }
}
</script>

<style scoped>
.configDialog {
  padding: 0 20px;
}
</style>
